package li.nibble.twittertido.adapter;

import java.util.ArrayList;

import li.nibble.twittertido.R;
import li.nibble.twittertido.beans.Comment;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

public class CommentAdapter extends ArrayAdapter<Comment> {

	ArrayList<Comment> comments;
	LayoutInflater vi;

	public CommentAdapter(Context context, int textViewResourceId,
			ArrayList<Comment> objects) {
		super(context, textViewResourceId, objects);
		this.comments = objects;
		vi = (LayoutInflater) getContext().getSystemService(
				Context.LAYOUT_INFLATER_SERVICE);
	}

	public static class ViewHolder {
		public TextView comment;
		public TextView user;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		View v = convertView;

		final ViewHolder holder;

		if (v == null) {
			v = vi.inflate(R.layout.comment_row, null);
			holder = new ViewHolder();
			holder.user = (TextView) v.findViewById(R.id.username);
			holder.comment = (TextView) v.findViewById(R.id.comment);
			v.setTag(holder);
		} else {
			holder = (ViewHolder) v.getTag();
		}

		Comment comment = comments.get(position);

		holder.user.setText(comment.getUsername());
		holder.comment.setText(comment.getComment());

		return v;
	}

}
